package com.example.accessibilitydemo.util;

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;

public class ActivityManagerUtil {

	/**
	 * 杀死后台进程
	 * 
	 * @param context
	 * @param packageName
	 */
	public static void killApp(Context context, String proccessName) {
		// 获取一个ActivityManager 对象
		ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		// 获取系统中所有正在运行的进程
		List<RunningAppProcessInfo> appProcessInfos = am.getRunningAppProcesses();
		// 对系统中所有正在运行的进程进行迭代
		for (RunningAppProcessInfo process : appProcessInfos) {
			String name = process.processName;
			if (name.equals(proccessName)) {
				am.killBackgroundProcesses(name);
			}
		}
	}

	/**
	 * 杀死后台进程
	 * 
	 * @param context
	 * @param packageName
	 */
	public static void killApp(String packageName) {
		ToolAdb.execShell(String.format("am force-stop  %s", packageName));
	}
}
